package tech.yangzhijie.blog.admin.controller;

import org.springframework.web.bind.annotation.*;
import tech.yangzhijie.blog.admin.entity.SystemUser;
import tech.yangzhijie.blog.admin.service.SystemUserService;

import java.util.List;

/**
 * @Author: 探险家劫
 * @Date: 2021/11/18 20:32
 */
@RestController
@RequestMapping("/user")
public class SystemUserController {

    private final SystemUserService service;

    public SystemUserController(SystemUserService service) {
        this.service = service;
    }

    @GetMapping
    public List<SystemUser> get(){
        return service.list();
    }

    @PostMapping
    public String save(SystemUser systemUser){
        return service.save(systemUser)?"添加成功！":"添加失败！";
    }

    @PutMapping
    public String update(SystemUser updatedUser){
        SystemUser systemUser = service.getById(updatedUser.getId());
        if(systemUser == null) return "修改失败！不存在该用户";
        return service.updateById(updatedUser)?"修改成功":"修改失败";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        return service.removeById(id)?"删除成功":"删除失败";
    }
}
